package BackTrack.BackTrack.combine;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LetterCombinations
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-17 9:45
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class LetterCombinations {

    /**
     * 17. 电话号码的字母组合
     * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
     * <p>
     * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
     * <p>
     * 示例 1：
     * <p>
     * 输入：digits = "23"
     * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
     *
     * @param digits
     * @return
     */
    //设置全局列表存储最后的结果
    List<String> list = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return list;
        }

        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        backTrackint(digits, numString, 0);
        return list;
    }

    StringBuilder stringBuilder = new StringBuilder();

    private void backTrackint(String digits, String[] numString, int index) {
        if (index == digits.length()) {
            list.add(stringBuilder.toString());
            return;
        }
        String str = numString[digits.charAt(index) - '0'];
        for (int i = 0; i < str.length(); i++) {
            stringBuilder.append(str.charAt(i));
            backTrackint(digits, numString, index + 1);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }


    public static void main(String[] args) {
        LetterCombinations li = new LetterCombinations();
        System.out.println(li.letterCombinations("23"));
    }
}